package servlet;

import java.io.IOException;
import java.io.PrintWriter;

import javax.servlet.ServletConfig;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import dao.CustomerDao;
import dao.impl.CustomerDaoImpl;
import pojo.Customer;

/**
 * Servlet implementation class RegisterServlet
 */
public class RegisterServlet extends HttpServlet {
	private static final long serialVersionUID = 1L;
       
    /**
     * @see HttpServlet#HttpServlet()
     */
    public RegisterServlet() {
        super();
        // TODO Auto-generated constructor stub
        System.out.println("RegisterServlet--------------");
    }

    @Override
    public void init(ServletConfig config) throws ServletException {
    	// TODO Auto-generated method stub
    	super.init(config);
    	String enc=config.getInitParameter("encoding");
    	System.out.println("---"+enc);
    }
    
	/**
	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
		doPost(request, response);
	}

	/**
	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
		//整个程序任何地方都可以调用
		this.getServletContext().setAttribute("qq", "qq");
		
		request.setCharacterEncoding("utf-8");
		response.setCharacterEncoding("utf-8");
		response.setHeader("Content-type", "text/html;charset=UTF-8");
		CustomerDao customerDao=new CustomerDaoImpl();
	
		String name=request.getParameter("name");
		
	//	name=new String(name.getBytes("ISO-8859-1"),"GBK");
		
		int age=Integer.parseInt(request.getParameter("age"));
		Customer customer=new Customer();
		customer.setName(name);
		customer.setAge(age);
		
		PrintWriter writer= response.getWriter();
		
		int a=customerDao.addCustomer(customer);
		if(a>0){
		writer.write("插入成功");
		
		}else{
		writer.write("插入失败");
			
		}
		writer.close();
	}

}
